package com.mashibing.juc.c_021_02_AQS;

import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * @author: Yuanzt
 * @Date: 2021/3/31
 */
public class Sync extends AbstractQueuedSynchronizer {

    @Override
    protected boolean tryAcquire(int arg) {
        if (compareAndSetState(0,1)){
            setExclusiveOwnerThread(Thread.currentThread());
        }
        return false;
    }

    @Override
    protected boolean tryRelease(int arg) {
        setExclusiveOwnerThread(null);
        setState(0);
        return true;
    }

    @Override
    protected boolean isHeldExclusively() {
        return getState()==1;
    }
}
